# 下标索引
# []查相应下表的元素 数据容器.index()相应元素的下标
a = "it hei ma"
print(a[0])
print(a[2])
print(a[3])
print(a[-1])
my_str = " 12itcast and itheima21 "
print(my_str.index("a"))
# replace  方法#把字符串1换成字符串2
b = my_str.replace("it", "程序")
print(my_str)
print(b)
# split() 方法, 把字符串按照括号内的字符串进行分割，并得到一个新的列表
c = my_str.split("i")
print(c)
# z字符串的规整操作
print(my_str.strip())
d = my_str.strip().strip("1" "2")
print(d)
for x in my_str:
    print(x)
y = 0
while y < len(my_str):
    print(my_str[y])
    y += 1
my_str1 = "itheima itcast boxuegu"
count = my_str1.count("it")
n = my_str1.replace(" ", "|")
q = n.split("|")
print(f"字符串itheima itcast boxuegu中有：{count}个it 字符")
print(f"字符串itheima itcast boxuegu被替换空格后，结果，{n}")
print(f"字符串itheima itcast boxuegu按照|分割后，得到{q}")
# 数据容器（序列）的切片操作
qie_pan_1 = my_str1[0::1]
print(qie_pan_1)
my_list = [0, 1, 2, 3, 4, 5, 6]
result1 = my_list[1:4:1]  # 步长是1可省略不写
print(result1)
my_tuple = (0, 1, 2, 3, 4, 5, 6, )
result2 = my_tuple[:]
print(result2)
str1 = "0123456"
result3 = str1[::2]
print(result3)
result4 = str1[::-1]  # 也可以写成[-1::-1]
print(result4)
result5 = my_list[3:1:-1]
print(result5)
result6 = my_tuple[::-2]
print(result6)
# 练习
str2 = "万过薪月，员序程马黑来，nohtyP学"
hei = int(str2[::-1].index('黑'))
yuan = int(str2[::-1].index('员'))
str3 = str2[::-1]  # 还能优化
str4 = str3[hei:yuan+1:1]
print(str4)

# 集合
set2 = {'Python', '黑马程序员', '你好呀'}
set2.add('你是我的眼')
element = set2.pop()
print(set2)
print(element)
set2.clear()    # 清空集合
print(set2)
# 差集
set3 = {1, 2, 3, 6, 8}
set4 = {2, 4, 6, 8}
set5 = set3.difference(set4)
print(set5)
set3.difference_update(set4)
print(set3)
print(set4)
set6 = set3.union(set4)  # 集合1.union(集合2) 不修改原有集合，得到新集合
print(set6)
len(set6)
print(len(set6))
for x in set6:
    print(x)
#  集合作业
empty = set()
list1 = ['黑马程序员', '传智播客', '黑马程序员', '传智播客', 'itheima', 'itcast', 'itheima', 'itcast', 'best']
for v in list1:
    empty.add(v)
print(empty)
